package com.bigfish.gbs.service.impl;


import com.bigfish.gbs.dao.UserMapper;
import com.bigfish.gbs.dto.Account;
import com.bigfish.gbs.entity.User;
import com.bigfish.gbs.service.AccountService;
import com.bigfish.gbs.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

/* *
 * @Author Joycessh
 * @Description 
 * @Date 22:04 2018/3/7
 */
@Service("AccountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Override
    public Account loadAccount(String appId) throws DataAccessException {
        User user = userMapper.selectByUniqueKey(appId);
        return user != null ? new Account(user.getUserId(),user.getPassword(),user.getSalt()) : null;
    }

    @Override
    public boolean isAccountExistByUid(String userId) {
        User user = userMapper.selectByPrimaryKey(userId);
        return user != null ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public boolean registerAccount(User account) throws DataAccessException {

        // 给新用户授权访客角色
        userService.authorityUserRole(account.getUserId(),103);

        return userMapper.insertSelective(account) ==1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public String loadAccountRole(String appId) throws DataAccessException {

        return userMapper.selectUserRoles(appId);
    }

    @Override
    public boolean registerSupplier(User account) {
        // 给新用户授权商家角色
        userService.authorityUserRole(account.getUserId(),104);

        return userMapper.insertSelective(account) ==1 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public boolean registerAdmin(User account) {
        // 给新用户授权管理员角色
        userService.authorityUserRole(account.getUserId(),102);

        return userMapper.insertSelective(account) ==1 ? Boolean.TRUE : Boolean.FALSE;
    }
}
